<?php

namespace app\admin\service\admin;

use app\admin\model\admin\AdminDepartment;
use app\admin\service\BaseService;

class DepartmentService extends BaseService
{
    public function __construct(AdminDepartment $model)
    {
        $this->model = $model;
    }

    public function getDataList(array $where, $field = '*', string $order = 'sort asc,id asc'): array
    {
        $model = $this->model->where($where)->field($field);
        $list = $model->order($order)->select()->toArray();
        $count = $model->count();

        return compact('list', 'count');
    }

    public function getDeptList(): array
    {
        return $this->model->where('status', 1)->select()->toArray();
    }

    public function addDept(array $data)
    {
        $getId = $this->model->insertGetId($data);
        if ($getId) {
            return json(['code' => 1, 'msg' => '保存成功', 'data' => $this->getDeptList()]);
        } else {
            return json(['code' => -1, 'msg' => '保存失败']);
        }
    }

    public function editDept(array $data)
    {
        $getId = $data['id'];
        $menuInfo = $this->model->find($getId);
        unset($data['id']);
        $menuInfo->save($data);

        return json(['code' => 1, 'msg' => '保存成功']);
    }

    public function delDept($data)
    {
        $this->model::destroy($data['id']);
        return json(['code' => 1, 'msg' => '删除成功']);
    }

}